var _ = require('./../../FrameWork/we_underscore');
var TimeOutCtr = require('./../../FrameWork/Component/TimeOutCtr');
var EnumPkRoom = require('./../Enum/EnumPkRoom');
var ClientDelegate = require('./../../FrameWork/Delegate/ClientDelegate');
var ClientEveDelegate = require('./../../FrameWork/Delegate/ClientEveDelegate');
var ClientPkDelegateType = require('./../Delegate/ClientPkDelegateType');
var UtilGameObject = require('./../../FrameWork/Utils/UtilGameObject');
cc.Class({
    extends: ClientDelegate,
    properties: {
        posFixed: {
            default: null,
            type: cc.Node,
            displayName: '固定牌位置'
        },
        prefabFixed: {
            default: null,
            type: cc.Prefab,
            displayName: '固定牌预设'
        },
        posShowBottom: {
            default: null,
            type: cc.Node,
            displayName: '底牌表现位置'
        },
        prefabShowBottom: {
            default: null,
            type: cc.Prefab,
            displayName: '底牌表现预设'
        },
        posHeadList: [cc.Node]
    },
    onLoad() {
        this._super();
        this.showCardCtrList = [];
        this.fixedCardCtrList = []; //固定底牌
        this.addListerDelegate(ClientPkDelegateType.Server_addBottomCardsToHand, this.actBottonCards.bind(this));
        this.addListerDelegate(ClientPkDelegateType.Clinet_cleanBottomUI, this.resetNet.bind(this));
        this.resetNet();
    },
    resetNet() {
        _.each(this.fixedCardCtrList, function(_ctr) {
            if (_ctr && !!_ctr.node) {
                _ctr.node.destroy();
            }
        })
        _.each(this.showCardCtrList, function(_ctr) {
            if (_ctr && !!_ctr.node) {
                _ctr.node.destroy();
            }
        })
        this.showCardCtrList = [];
        this.fixedCardCtrList = [];
        this.createFixedCards();
        this.createShowBottomCards();
    },

    /**创建固定在顶部的底牌*/
    createFixedCards() {

        for (var i = 0; i < 3; ++i) {
            var ctr = UtilGameObject.getCtrByPrefabs(this.prefabFixed, this.posFixed);
            ctr.doLook(false);
            this.fixedCardCtrList.push(ctr);
        }
    },

    /***创建展现底牌对像 */
    createShowBottomCards() {
        var spaceX = 0;
        var prefabScale = this.prefabShowBottom.data.scale;
        var sizePrefab = cc.size(this.prefabShowBottom.data.width * prefabScale, this.prefabShowBottom.data.height * prefabScale);
        var totalWidth = sizePrefab.width * 3 + spaceX * 2;
        var firstX = -totalWidth * 0.5 + sizePrefab.width * 0.5;
        for (var i = 0; i < 3; ++i) {
            var ctr = UtilGameObject.getCtrByPrefabs(this.prefabShowBottom, this.posShowBottom);
            ctr.setPositionX(firstX + (sizePrefab.width + spaceX) * i);
            ctr.setScale(0.7);
            this.showCardCtrList.push(ctr);
        }
    },

    /***动画播放收牌 */
    actBottonCards(_info) {
        var seatIdx = _info.seatIdx;
        if (0 == seatIdx) {
            //这边是加到自己手上
            var _cardIds = _info.cardIds;
            var size = _.size(_cardIds);
            _.each(_cardIds, function(_id, _idx) {
                var worldPos = this.showCardCtrList[_idx].node.convertToWorldSpaceAR(cc.v2(0, 0))
                var acts = this.actSingleBottomCard(_idx * 0.1, this.showCardCtrList[_idx], _id, function(_idxCb, _scaleCb) {
                    this.fixedCardCtrList[_idxCb].refreshUIByCardId(_id);
                    this.showCardCtrList[_idxCb].refreshUIByCardId(_id);
                    ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_addBottomOneCardToHand, {
                        worldPos: worldPos,
                        cardId: _id,
                        idx: size - _idxCb,
                        scale: _scaleCb
                    });
                }.bind(this), _idx);
                this.showCardCtrList[_idx].doAction(acts);
            }.bind(this));
        } else {
            //这边是加到自己手上
            var _cardIds = _info.cardIds;
            var size = _.size(_cardIds);
            _.each(_cardIds, function(_id, _idx) {
                this.actSingleBottomCardToOther(_idx * 0.1, _id, _idx, seatIdx);
            }.bind(this));
        }
    },

    actSingleBottomCard(delatTime, ctr, _id, cb, _idx) {
        if (null == this._tmpScale) {
            this._tmpScale = ctr.getScale();
        }
        var scale = this._tmpScale + 0.1;
        var scaleT = 0.2;
        var acts = [
            cc.delayTime(delatTime),
            cc.scaleTo(scaleT, 0, scale),
            cc.callFunc(function() {
                cb(_idx, scale);
            }),
        ];
        var act = cc.sequence(acts);
        return act;
    },

    actSingleBottomCardToOther(delatTime, _id, _idx, _seatIdx) {
        var worldPos = this.posHeadList[_seatIdx].convertToWorldSpaceAR(cc.v2(0, 0));
        var endPos = this.showCardCtrList[_idx].node.parent.convertToNodeSpaceAR(worldPos)
        var scale = 0.7;

        if (null == this._tmpScale) {
            this._tmpScale = this.showCardCtrList[_idx].getScale();
        }
        scale = this._tmpScale + 0.1;

        var actTime = 0.3;
        var acts = [
            cc.delayTime(delatTime),
            cc.scaleTo(actTime, 0, scale),
            cc.callFunc(function() {
                this.fixedCardCtrList[_idx].refreshUIByCardId(_id);
                this.showCardCtrList[_idx].refreshUIByCardId(_id);
            }.bind(this)),
            cc.scaleTo(actTime, scale, scale),
            cc.delayTime(1 - delatTime),

            cc.spawn(
                cc.moveTo(actTime, endPos),
                cc.scaleTo(actTime, 0.5),
                cc.sequence(cc.delayTime(actTime * 0.7), cc.fadeOut(actTime * 0.3))
            ),
            cc.callFunc(function() {

                ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_refreshCardLeftCnt, {
                    seatIdx: _seatIdx,
                    cardLeftCnt: 18 + _idx
                });


            }.bind(this)),


        ];
        var act = cc.sequence(acts);
        this.showCardCtrList[_idx].doAction(act);
    }
});